/**
 * KudoStar Map Activity
 */
package com.kudostar.android.kudostar;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.View;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayController;
import com.google.android.maps.Point;

/**
 * @author mike
 * 
 */
public class KudoMap extends MapActivity {

	protected ArrayList<Kudo> allKudos = new ArrayList<Kudo>(); 
	protected MapView mMap = null;
  
    protected OverlayController myOverlayController = null;

    protected LocationManager lm = null; 
    @Override
    
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView( R.layout.view_map );
        mMap = (MapView) findViewById( R.id.kudomap );
        findViewById( R.id.close ).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				finish( );
			}
		} );
        findViewById( R.id.zoom_in).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				int level = mMap.getZoomLevel();
				mMap.getController().zoomTo(level + 1);
			}
		} );
        findViewById( R.id.zoom_out).setOnClickListener( new View.OnClickListener( ) {
			public void onClick(View view) {
				int level = mMap.getZoomLevel();
				mMap.getController().zoomTo(level - 1);
			}
		} );
        //Set default zoom
        mMap.getController().zoomTo(4);
        OverlayController controller = mMap.createOverlayController();
        controller.add(new KudoMapOverlay(), true);

	    lm = (LocationManager)getSystemService( Context.LOCATION_SERVICE ); 
	    List<LocationProvider> providers = lm.getProviders();
	    LocationProvider provider = providers.get(0); 
	    // Get Location and center map to it
	    Location curLoc = lm.getCurrentLocation( provider.getName() );
	    Double lat = curLoc.getLatitude() * 1E6;
	   	Double lng = curLoc.getLongitude() * 1E6; 
	   	Point point = new Point( lat.intValue(), lng.intValue() );
	   	mMap.getController().animateTo( point );
        this.refreshKudoList( );
    } 
    private void refreshKudoList( ) {
    	switch( Globals.list_type ) {
    		case 1:
    			//get my kudos
			break;
    		case 2:
    			//get friends kudos
			break;
    	}
    	Location kLocation1 = new Location( );
    	Location kLocation2 = new Location( );
    	Location kLocation3 = new Location( );
    	
    		kLocation1.setLatitude( 47.480088 );
    		kLocation1.setLongitude( -122.321777 );
    		Kudo k1 = new Kudo( );
    		k1.setLocation(  kLocation1 );
    		k1.setValue( 1 );
    		allKudos.add( k1 );

    		kLocation2.setLatitude( 33.421914 );
    		kLocation2.setLongitude( -111.940126 );
    		Kudo k2 = new Kudo( );
    		k2.setLocation(  kLocation2 );
    		k2.setValue( 3 );
    		allKudos.add( k2 );
    		
    		kLocation3.setLatitude( 33.395332 );
    		kLocation3.setLongitude( -111.737823 );
    		Kudo k3 = new Kudo( );
    		k3.setLocation(  kLocation3 );
    		k3.setValue( -2 );
    		allKudos.add( k3 );
    }
    protected class KudoMapOverlay extends Overlay {

    	public KudoMapOverlay() {

    	}

    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see com.google.android.maps.Overlay#draw(android.graphics.Canvas,
    	 *      com.google.android.maps.Overlay.PixelCalculator, boolean)
    	 */
    	@Override
    	public void draw(Canvas canvas, PixelCalculator calculator, boolean shadow) {
    		// TODO Auto-generated method stub
    		super.draw( canvas, calculator, shadow );
    		int[] kudoScreenCoords = new int[2]; 
            for(Kudo aKudo : KudoMap.this.allKudos) { 
            	if( aKudo.getValue(  ) != 0 ) {
	            	Location starLocation = aKudo.getLocation();
	            	Double lat = starLocation.getLatitude() * 1E6;
	            	Double lng = starLocation.getLongitude() * 1E6; 
	            	Point point = new Point( lat.intValue(), lng.intValue() );
	                // Converts lat/lng-Point to coordinates on the screen.
	                calculator.getPointXY(point, kudoScreenCoords);
	                /**
	                 * Draw here
	                 */
	                Drawable starImage = null;
	                switch( aKudo.getValue(  ) ) {
	                	case -3:
	                		starImage = KudoMap.this.getResources().getDrawable( R.drawable.red_3_marker );
	                	break;
	                	case -2:
	                		starImage = KudoMap.this.getResources().getDrawable( R.drawable.red_2_marker );
	                	break;
	                	case -1:
	                		starImage = KudoMap.this.getResources().getDrawable( R.drawable.red_1_marker );
	                	break;
	                	case 1:
	                		starImage = KudoMap.this.getResources().getDrawable( R.drawable.green_1_marker );
	                	break;
	                	case 2:
	                		starImage = KudoMap.this.getResources().getDrawable( R.drawable.green_2_marker );
	                	break;
	                	case 3:
	                		starImage = KudoMap.this.getResources().getDrawable( R.drawable.green_3_marker );
	                	break;
	                }
	                int xpos = kudoScreenCoords[0]-(starImage.getIntrinsicWidth()/2);
	                int ypos = kudoScreenCoords[1]-(starImage.getIntrinsicHeight() );
	                starImage.setBounds( xpos, ypos, xpos + starImage.getIntrinsicWidth(), ypos + starImage.getIntrinsicHeight());           
	                starImage.draw(canvas);
            	}
            }
    	}

    }
}